package com.alphawavesgames.engine.utilities;

/**
 *
 * @author Andrew Dyer
 */
public class ArrayMapUtilities {

    /**
     * Finds and reports the order of points (x, y) to display from top to
     * bottom of a grid tilted 45 degrees clockwise. The x-axis runs along the
     * top right side, and the y-axis along the top left. The origin is at the
     * top tip with x-values running to the right and y-values to the left.
     *
     * @param numX The width of the grid.
     * @param numY The height of the grid.
     * @return The points in order from the tip at the top to the point at the
     * bottom. The points are given in the form [x1, y1, x2, y2, ...].
     */
    public static int[] getDisplayOrder(int numX, int numY) {
        int[] points = new int[(numX * numY) << 1];

        int i = 0;

        int highest = Math.max(numX, numY);
        int lowest = Math.min(numX, numY);

        int lines = highest + lowest - 1;

        for (int line = 0; line < lines; line++) {
            if (line < lowest) { // lines are getting longer
                for (int col = 0; col <= line; col++) {
                    // (x, y) = (col, line - col)
                    points[i] = col;
                    points[i + 1] = line - col;
                    i += 2;
                }
            } else if (line >= highest) { // lines are getting shorter
                for (int col = numX - (lines - line); col < numX; col++) {
                    // (x, y) = (col, numY - 1 - col + numX - lines + line)
                    points[i] = col;
                    points[i + 1] = numY - 1 - col + numX - lines + line;
                    i += 2;
                }
            } else { // lines keep the same length
                for (int col = 0; col < lowest; col++) {
                    if (numX > numY) {
                        // (x, y) = (col + numX - (lines - line), numY - col - 1)
                        points[i] = col + numX - (lines - line);
                        points[i + 1] = numY - col - 1;
                    } else {
                        // (x, y) = (col, line - col)
                        points[i] = col;
                        points[i + 1] = line - col;
                    }
                }
            }
        }
        return points;
    }
}
